//
//  ReleaseServerViewController.m
//  PYWGetColor
//
//  Created by 杜文杰 on 2018/6/11.
//  Copyright © 2018年 dwj. All rights reserved.
//

#import "ReleaseServerViewController.h"
#import "ReleaseServerCollectionViewCell.h"
#import "HeaderViewCollectionReusableView.h"
#import "SearchServerOneViewController.h"
#import "ChooseServerTypeViewController.h"

@interface ReleaseServerViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *shopingCollectionView;
@property (nonatomic, strong) NSMutableArray *datamuArray;
@end

@implementation ReleaseServerViewController
/**
 获取服务大分类接口
 参数名    必选    类型    说明
 uid    是    string    uid
 token    是    string    秘钥
 parentcat    是    int    上级分类id，所有一级分类的上级分类id为0
 */
- (void)getServersList {
    [[AFAppDotNetAPIClient sharedClient] POST:@"interface.php?m=servicecat" parameters:@{@"uid":[UserModel sharedInstanced].userId,@"token":[UserModel sharedInstanced].token,@"parentcat":@0} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"error_code"] isEqualToString:@"0"]) {
//            ShowMessage(@"操作成功");
            self.datamuArray = responseObject[@"data"];
            [self.shopingCollectionView reloadData];
        }else{
            ShowMessage(@"操作失败");
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self showError:error];
    }];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    self.title = @"发布服务";
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self getServersList];
    [self.view addSubview:self.shopingCollectionView];
    __weak typeof(self)weakself = self;
    
    [self.shopingCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakself.view);
        make.left.right.equalTo(weakself.view);
        make.height.mas_equalTo(SCREEN_HEIGHT - SafeAreaTopHeight - SafeAreaBottomHeight);
//        make.height.mas_equalTo(200);
    }];
}

#pragma mark - UICollectionViewDelegate and UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.datamuArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ReleaseServerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ReleaseServerCollectionViewCell class]) forIndexPath:indexPath];
    cell.dataDic = self.datamuArray[indexPath.row];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ChooseServerTypeViewController *VC = [[ChooseServerTypeViewController alloc]init];
    VC.dataDic = self.datamuArray[indexPath.row];
    [self.navigationController pushViewController:VC animated:NO];
    
}
// 设置头尾部内容(多数情况可以不用)
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableView =nil;
    
    if (kind == UICollectionElementKindSectionHeader) {
        //定制头部视图的内容
        HeaderViewCollectionReusableView *headerV = (HeaderViewCollectionReusableView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
        headerV.searchBtnBlock = ^{
            NSLog(@"头视图回调");
            SearchServerOneViewController *VC = [[SearchServerOneViewController alloc]init];
            [self.navigationController pushViewController:VC animated:NO];
        };
        reusableView = headerV;
    }
    return reusableView;
}
- (UICollectionView *)shopingCollectionView
{
    if (!_shopingCollectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH / 3,100 / HEIGHT_6S_SCALE);
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.sectionInset = UIEdgeInsetsMake(10 / HEIGHT_6S_SCALE, 0, 0, 0);
        // 设置头部并给定大小(多数情况可以不用)
        [flowLayout setHeaderReferenceSize:CGSizeMake(SCREEN_HEIGHT,84)];
        
        _shopingCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _shopingCollectionView.backgroundColor = [UIColor clearColor];
        _shopingCollectionView.bounces = YES;
        _shopingCollectionView.delegate = self;
        _shopingCollectionView.dataSource = self;
        _shopingCollectionView.showsVerticalScrollIndicator = NO;
        _shopingCollectionView.showsHorizontalScrollIndicator = NO;
        _shopingCollectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [_shopingCollectionView registerClass:[ReleaseServerCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([ReleaseServerCollectionViewCell class])];
        // 注册头部视图 (多数情况可以不用)
        [_shopingCollectionView registerClass:[HeaderViewCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
    }
    
    return _shopingCollectionView;
}
- (NSMutableArray *)datamuArray
{
    if (!_datamuArray) {
        _datamuArray = @[].mutableCopy;
    }
    return _datamuArray;
}
@end
